package com.guardchina.framework.web.plugins.validator;

import com.guardchina.framework.util.utils.CheckUtil;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 描述：校验身份证
 *
 * @author lwb
 * @date 2019/5/24 13:18
 */
public class CheckIdNumValidator implements ConstraintValidator<CheckIdNum, String> {

    private boolean enableBlank;

    @Override
    public void initialize(CheckIdNum constraintAnnotation) {
        this.enableBlank = constraintAnnotation.enableBlank();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(enableBlank && StringUtils.isBlank(value)){
            return true;
        }
        return CheckUtil.isIdNum(value);
    }
}
